with Interfaces.C;  use Interfaces.C;
with Managed.Alarm; use Managed.Alarm;

with Ada.Strings.Fixed;
with GWindows.GStrings;
with GWindows.Multi_Media;

separate(ffxialarm.tasks)
package body Alarm_Routines is

  procedure Play_and_Repeat is
    type LASTINPUTINFO is record
      cbSize : Interfaces.C.Unsigned := LASTINPUTINFO'Size/8;
      dwTime : Interfaces.C.Unsigned_long;
    end record;
    type PLASTINPUTINFO is access all LASTINPUTINFO;

    function GetLastInputInfo(plii : in PLASTINPUTINFO) return Interfaces.C.Int;
    pragma Import(Stdcall, GetLastInputInfo, "GetLastInputInfo");

    function GetTickCount return Interfaces.C.Unsigned_Long;
    pragma Import(Stdcall, GetTickCount, "GetTickCount");

    plii  : aliased LASTINPUTINFO;
    Dummy : Interfaces.C.Int;
    STime : Interfaces.C.Unsigned_Long;

    procedure Play is
      use GWindows.Multi_Media;
      use GWindows.GStrings;
      use Ada.Strings.Fixed;
    begin
      Play_Sound_from_File -- Plays alarmX.wav
        ("alarm"&To_GString_from_String(Ada.Strings.Fixed.Trim (
         Integer'Image (Command.Retrieve_Alarm), Ada.Strings.Left))&".wav");
      delay 0.5;
      Dummy := GetLastInputInfo(plii'access);
    end Play;
  begin
    STime := GetTickCount;
    --Put_Line("Alarm1!");
    Play;
    while Managed.Config.Repeat
      and Command.Retrieve = Play
      and plii.dwTime < STime loop
      --Put_Line("Alarm!");
      Play;
    end loop;
  end Play_and_Repeat;

end Alarm_Routines;
